const app = getApp()
const db = wx.cloud.database()
const _ = db.command
const $ = db.command.aggregate

let floor = null
let timeSlot = null
Page({
  data: {
    date: '请点击选择'
  },
  onLoad(opt) {
    floor = opt.floor ? opt.floor : null
    timeSlot = opt.timeSlot ? opt.timeSlot : null
    console.log('预定页onload', floor, timeSlot)
    this.setData({
      floor: floor
    })
    if (floor) {
      wx.setNavigationBarTitle({
        title: floor + '可预定的教室',
      })
    }
    if (timeSlot) {
      wx.setNavigationBarTitle({
        title: '空余教室预约',
      })
    }
  },
  onShow() {
    this.setData({
      date: app._getNianYuiRi()
    }, success => {
      this.getList()
    })
  },
  getList() {
    if (this.data.date == '请点击选择') {
      wx.showToast({
        icon: 'error',
        title: '请选择日期',
      })
      return
    }
    if (floor) {
      db.collection('classVenue').aggregate()
        .match({
          floor: floor,
          timeList: _.not(_.eq(this.data.date))
        })
        .group({
          _id: '$name',
          num: $.sum(1)
        })
        .sort({
          _id: 1
        })
        .end()
        .then(res => {
          console.log(floor + '可预定教室列表', res)
          this.setData({
            list: res.list
          })
        })
    } else {
      db.collection('classVenue').aggregate()
        .match({
          timeSlot: timeSlot,
          timeList: _.not(_.eq(this.data.date))
        })
        .group({
          _id: {
            floor: '$floor',
            name: '$name',
            id: '$_id'
          }
        })
        .sort({
          _id: 1
        })
        .end()
        .then(res => {
          console.log('按时间段数据', res)
          this.setData({
            list: res.list
          })
        })
    }

  },
  // 选择日期
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    }, success => {
      if (app._getNianYuiRi() <= this.data.date) {
        this.getList()
      } else {
        this.setData({
          list: null
        })
        wx.showModal({
          title: "提示",
          content: "只可以选择今日或者以后的日期，之前的日期没法选择",
          showCancel: false
        })
      }

    })
    console.log()
  },
  //去单个教室预约页
  goDetail(e) {
    let item = e.currentTarget.dataset.item
    if (floor) {
      wx.navigateTo({
        url: '/pages/class-reserveDetail/class-reserveDetail?name=' + item._id + "&date=" + this.data.date + "&floor=" + floor,
      })
    }

  },
  //添加预定
  addOrder(e) {
    let item = e.currentTarget.dataset.item._id
    let user = app.globalData.userInfo
    let dataList = []
    dataList.push(item.id)

    db.collection('classOrder').add({
      data: {
        venueFloor: item.floor,
        venueName: item.name,
        venueId: item.id,
        timeSlot: timeSlot,
        date: this.data.date,
        name: user.name,
        phone: user.phone,
        type: user.type,
        department: user.department,
        status: '待使用'
      }
    }).then(res => {
      console.log('添加预定成功', res)
      // 全部预定添加成功以后，去改变教室状态
      wx.cloud.callFunction({
        name: 'classVenue',
        data: {
          action: 'reserve',
          list: dataList,
          date: this.data.date
        }
      }).then(res => {
        console.log('修改教室状态结果', res)
        wx.showToast({
          title: '预定成功',
        })
        this.getList()
      }).catch(res => {
        console.log('失败', res)
        wx.showToast({
          icon: 'error',
          title: '预定失败',
        })
      })
    })


  }
})